package cn.didi.feidi.androidorder.assembly;


import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import cn.didi.feidi.androidorder.R;


/**
 * Created by king on 2017/7/5.
 */

public class FeidiActionBar extends LinearLayout {
    private String TAG = "FeidiActionBar";
    private LayoutParams lp;
    private ProgressBar mProgressBar;
    private TextView tvTitle;


    //可变按钮
    private ImageView btnFallback;
    private TextView btnMessage;
    private TextView btnSetting;
    private ImageView btnScanCode;



 //    private Button mHomeBtn;
//    private Button mSubMenuBtn;
    private FeidiActionBarCallBack mBarCallBack;

    //    private ChaoSearchEditText searchEditText;
    private OnClickListener mClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.action_bar_fallback:
                    if (mBarCallBack != null) {
                        mBarCallBack.onFallbackClickCallBack(btnFallback);
                    } else {
                        ((Activity) getContext()).finish();
                    }
                    break;
                case R.id.action_bar_messge:
                    if (mBarCallBack != null) {
                        mBarCallBack.onMessageClickCallBack(btnMessage);
                    } else {
                        Toast.makeText(getContext(), "未设置", Toast.LENGTH_SHORT)
                                .show();
                    }
                    break;
                case R.id.action_bar_setting:
                    if (mBarCallBack != null) {
                        mBarCallBack.onSettingClickCallBack(btnSetting);
                    } else {
                        Toast.makeText(getContext(), "未设置", Toast.LENGTH_SHORT)
                                .show();
                    }
                    break;
                case R.id.action_bar_scancode:
                    if (mBarCallBack != null) {
                        mBarCallBack.onScanCodeCallBack(btnScanCode);
                    } else {
                        ((Activity) getContext()).finish();
                    }
                default:
                    break;
            }
        }
    };

    public FeidiActionBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public FeidiActionBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FeidiActionBar(Context context) {
        super(context);
        init();
    }

    public void addChaoActionBarCallBack(FeidiActionBarCallBack callBack) {
        this.mBarCallBack = callBack;
    }

    public void setDefaultBackgroundColor() {
//        findViewById(R.id.action_bar_layout).setBackgroundColor(
//                getResources().getColor(R.color.actionbar_background));

        findViewById(R.id.action_bar_layout).setBackgroundResource(R.drawable.view_action_bar_bg_blue_gradient);
        btnMessage.setTextColor(getResources().getColor(R.color.actionbar_background_default));
        tvTitle.setTextColor(getResources().getColor(R.color.actionbar_background_default));
        btnSetting.setTextColor(getResources().getColor(R.color.actionbar_background_default));
    }

    public void setNullBackgroundColor() {

        findViewById(R.id.action_bar_layout).setBackgroundResource(R.drawable.view_action_bar_bg_null);
        btnMessage.setTextColor(getResources().getColor(R.color.actionbar_background_null));
        tvTitle.setTextColor(getResources().getColor(R.color.actionbar_background_null));
        btnSetting.setTextColor(getResources().getColor(R.color.actionbar_background_null));
    }

    public void setGrayBackgroundColor() {
        findViewById(R.id.action_bar_layout).setBackgroundColor(
                getResources().getColor(R.color.theme_background_light_gray));
    }

    /**
     * View.VISIBLE
     *
     * @param visibility
     */
    public void setFallbackVisibility(int visibility) {
        btnFallback.setVisibility(visibility);
        findViewById(R.id.action_bar_fallback_parent).setVisibility(visibility);
    }


    /**
     * View.VISIBLE
     *
     * @param visibility
     */
    public void setMessageVisibility(int visibility) {
        btnMessage.setVisibility(visibility);
    }

    /**
     * View.VISIBLE
     *
     * @param visibility
     */
    public void setSettingVisibility(int visibility) {
        btnSetting.setVisibility(visibility);
//        if (visibility == View.INVISIBLE) {
//            setSubmenuIcon(0);
//        }
    }

//    /**
//     * setText(Html.fromHtml(text));
//     *
//     * @param text
//     */
//    public void setHomeText(String text) {
//        mHomeBtn.setText(Html.fromHtml(text));
//    }

//    public void setHomeIcon(Bitmap home_page_filter) {
//        if (home_page_filter == null) {
//            mHomeBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
//                    null);
//            return;
//        }
//
//        Drawable drawable = new BitmapDrawable(home_page_filter);
//        mHomeBtn.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null,
//                null);
//    }

//    public void setHomeIcon(int drawersrc) {
//        mHomeBtn.setCompoundDrawablesWithIntrinsicBounds(drawersrc, 0, 0, 0);
//    }

    /**
     * setText(text);
     *
     * @param text
     */
    public void setSettingText(String text) {
        btnSetting.setText(text);
    }

    /**
     * setScanCode（int visibilty）
     * @param visibility
     * 当底部导航栏跳回到首页时，使其扫描二维码图标显示
     */
    public void setScanCodeView(int visibility){
        btnScanCode.setVisibility(visibility);
        findViewById(R.id.action_bar_scancode_parent).setVisibility(visibility);
    }

    /**
     * setText(Html.fromHtml(text));
     *
     * @param text
     */
    public void setTitleText(String text) {
        if (text != null && !"".equals(text))
            tvTitle.setText(text);
    }

//    public void setSubmenuIcon(Bitmap home_page_filter) {
//        if (home_page_filter == null)
//            return;
//        Drawable drawable = new BitmapDrawable(home_page_filter);
//        mSubMenuBtn.setCompoundDrawablesWithIntrinsicBounds(null, null,
//                drawable, null);
//    }

//    public void setSubmenuIcon(int drawersrc) {
//        mSubMenuBtn.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawersrc, 0);
//    }

//    public void setSubmenuTopText(int topdrawersrc, String text) {
//        mSubMenuBtn.setCompoundDrawablesWithIntrinsicBounds(0, topdrawersrc, 0,
//                0);
//        mSubMenuBtn.setCompoundDrawablePadding(2);
//
//        int left = AppUtil.getDp(16, getResources());
//        int top = AppUtil.getDp(8, getResources());
//        int right = AppUtil.getDp(16, getResources());
//        int bottom = AppUtil.getDp(1, getResources());
//        mSubMenuBtn.setPadding(left, top, right, bottom);
//        setSubmenuText(text);
//    }

    private void init() {
        if (isInEditMode())
            return;

//        lp = new LayoutParams(LayoutParams.MATCH_PARENT, AppUtil.getDp(48,
//                getResources()));
//        setLayoutParams(lp);

        View view = LayoutInflater.from(getContext()).inflate(
                R.layout.feidi_action_bar, this);

//        searchEditText = (ChaoSearchEditText) view
//                .findViewById(R.id.edit_search);
        tvTitle = (TextView) view.findViewById(R.id.action_bar_title);
        btnFallback = (ImageView) view.findViewById(R.id.action_bar_fallback);
        btnMessage = (TextView) view.findViewById(R.id.action_bar_messge);
        btnSetting = (TextView) view.findViewById(R.id.action_bar_setting);
        btnScanCode = (ImageView) view.findViewById(R.id.action_bar_scancode); //gaidong
        view.findViewById(R.id.action_bar_fallback_parent).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onClick(v.findViewById(R.id.action_bar_fallback));
            }
        });


        mProgressBar = (ProgressBar) view
                .findViewById(R.id.action_bar_progress_circular);
        // mProgressBar.setBackgroundColor(getContext().getResources().getColor(R.color.midou_font_white));
        // mProgressBar.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.ic_launcher));
        showProgressBar(false);

        btnFallback.setOnClickListener(mClickListener);
        btnMessage.setOnClickListener(mClickListener);
        btnSetting.setOnClickListener(mClickListener);
        btnScanCode.setOnClickListener(mClickListener);

        // if(!Network.isConnectedOrConnecting(getContext())){
        // setGrayBackgroundColor();
        // }
    }


    public View getTitleView() {
        return tvTitle;
    }

    public View getFallbackView() {
        return btnFallback;
    }

    public View getMessageView() {
        return btnMessage;
    }

    public View getSettingView() {
        return btnSetting;
    }
    public View getScanCodeView(){
        return btnScanCode;

    }

    public ProgressBar getProgressBar() {
        return mProgressBar;
    }

    public void showProgressBar(boolean show) {
        if (mProgressBar != null)
            mProgressBar.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    public interface FeidiActionBarCallBack {
        public void onFallbackClickCallBack(View view);

        public void onMessageClickCallBack(View view);

        public void onSettingClickCallBack(View view);

        public void onScanCodeCallBack(View view);
    }
}
